<?php

require_once 'html/element.php';

class Html_Element_Group extends Html_Element
{

	/**
	 * @var array
	 */
	protected $_meta = array(
			'tag' => 'div',
			'render' => '@group.simple',
	);

	/**
	 * @var array
	 */
	protected $_elements = array();
	
	/**
	 * @param array
	 */
	public function setChildren($elements)
	{
		foreach ($elements as $element)
		{
			$this -> addElement($element);
		}
		return $this;
	}

	/**
	 * @param string $type
	 * @param string $name
	 * @param array $options
	 * @return Html_Element_Group
	 */
	public function addElement($type, $name = null, $options = array())
	{
		if (is_string($type))
		{

		}else
		if (is_array($type))
		{
			$name = $type['name'];
			$options = $type['options'];
			$type = $type['type'];
		}

		$this -> _elements[$name] = Nuo::getResource($type, 'element') -> setName($name) -> setOptions($options);
		return $this;
	}

	/**
	 * @param string $name
	 * @return Html_Element
	 */
	public function getElement($name)
	{
		return isset($this -> _elements[$name]) ? $this -> _elements[$name] : null;
	}

	/**
	 * @param string $name
	 * @return Html_Element_Group
	 */
	public function removeElement($name)
	{
		if (isset($this -> _elements[$name]))
		{
			unset($this -> _elements[$name]);
		}
		return $this;
	}

	/**
	 * get array of element
	 * @return array
	 */
	public function getElements()
	{
		return $this -> _elements;
	}

	/**
	 * check element exists
	 * @return bool
	 */
	public function hasElement()
	{
		return isset($this -> _elements[$name]);
	}

	/**
	 * @return array
	 */
	public function getValues()
	{
		$data = array();
		foreach ($this->_elements as $name => $element)
		{
			$data[$name] = $element -> getValue();
		}
		return $data;
	}

	/**
	 * set value
	 * @return Html_Element_Group
	 */
	public function setValues($values)
	{
		foreach ($this->_elements as $name => $element)
		{
			if ($element instanceof Html_Group)
			{
				$element -> setValues($values);
			}
			else
			{
				$element -> setValue(isset($values[$name]) ? $values[$name] : null);
			}
		}
		return $this;
	}

	/**
	 * check is valid data
	 * @param array $values
	 * @return bool
	 *
	 */
	public function isValid($values)
	{
		$flag = true;
		foreach ($this->_elements as $name => $element)
		{
			if ($element instanceof Html_Group)
			{
				if (!$element -> isValid($values))
				{
					$flag = false;
				}
			}
			else
			{
				if (!$element -> isValid(isset($values[$name]) ? $values[$name] : null))
				{
					$flag = false;
				}
			}
		}
		return $flag;
	}

	/**
	 * magic getter
	 */
	public function __get($name)
	{
		if (isset($this -> _elements[$name]))
		{
			return $this -> _elements[$name];
		}

		if (isset($this -> _meta[$name]))
		{
			return $this -> _meta[$name];
		}

		if (isset($this -> _attrib[$name]))
		{
			return $this -> _attrib[$name];
		}

		return null;
	}

}
